﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Janus.Cores.Configuration
{
    /// <summary>
    /// remp 配置
    /// </summary>
    public class EBSConfigSection : ConfigurationSection
    {
        internal const string SECTION_NAME = "ebs";
        private ClientElementCollection clients;

        /// <summary>
        /// 服务工厂服务注册配置
        /// </summary>
        [ConfigurationProperty(ServiceFactoryElementCollection.SECTION_NAME)]
        public ServiceFactoryElementCollection ServiceFactoriesSection
        {
            get { return (ServiceFactoryElementCollection)base[ServiceFactoryElementCollection.SECTION_NAME]; }
        }

        [ConfigurationProperty(ServiceElementCollection.SECTION_NAME)]
        public ServiceElementCollection ServicesSection
        {
            get { return (ServiceElementCollection)base[ServiceElementCollection.SECTION_NAME]; }
        }

        [ConfigurationProperty(ClientSection.SECTION_NAME)]
        public ClientElementCollection ClientElementsSection
        {
            get
            {
                if (clients == null)
                    clients = (ClientElementCollection)base[ClientSection.SECTION_NAME];
                return clients;
            }
        }

        /// <summary>
        /// 数据库配置
        /// </summary>
        [ConfigurationProperty(DatabaseSection.SECTION_NAME, IsDefaultCollection = false)]
        public DatabaseSection DatabaseSection
        {
            get
            {
                return (DatabaseSection)base[DatabaseSection.SECTION_NAME];
            }
        }

        /// <summary>
        /// 资源配置
        /// </summary>
        [ConfigurationProperty(ResouceSection.SECTION_NAME, IsDefaultCollection = false)]
        public ResouceSection ResouceSection
        {
            get
            {
                return (ResouceSection)base[ResouceSection.SECTION_NAME];
            }
        }

        [ConfigurationProperty(ApplicationSection.SECTION_NAME, IsDefaultCollection = false)]
        public ApplicationSection ApplicationSection
        {
            get { return (ApplicationSection)base[ApplicationSection.SECTION_NAME]; }
        }

        public T GetSection<T>(string sectionName) where T : ConfigurationSection
        {
              return (T)base[sectionName];
        }

        string database;
        public string DefaultDatabase
        {
            get
            {
                if (string.IsNullOrEmpty(database))
                    database = DatabaseSection.Default;
                if (string.IsNullOrEmpty(database) && System.Configuration.ConfigurationManager.ConnectionStrings.Count > 0)
                    database = System.Configuration.ConfigurationManager.ConnectionStrings[0].Name;
                return database;
            }
        }

    }
}
